package myjava.i_collection;

import java.util.Map;
import java.util.TreeMap;

/**
 * @Auther: cty
 * @Date: 2019/11/15 下午9:43
 * @Description: 测试TreeMap的使用
 * @version: 1.0
 */
public class I_TestTreeMap {
    public static void main(String[] args) {
        Map<Integer,String> tm1 = new TreeMap<>();
        tm1.put(20,"aa");
        tm1.put(3,"bb");
        tm1.put(6,"cc");

        //按照key递增的方式排序
        for(Integer key:tm1.keySet()){
            System.out.println(key+"---"+tm1.get(key));
        }

        System.out.println("#############################################");

        Map<Emp,String> tm2 = new TreeMap<>();
        tm2.put(new Emp(100,"张三",50000),"好小伙");
        tm2.put(new Emp(200,"李四",6000),"还不错");
        tm2.put(new Emp(150,"王五",5000),"不积极");
        tm2.put(new Emp(170,"赵六",6000),"开心果");

        for(Emp key:tm2.keySet()){
            System.out.println(key+"---"+tm2.get(key));
        }
    }
}

class Emp implements Comparable<Emp>{
    int id;
    String name;
    double salary;

    public Emp(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int compareTo(Emp o) {
        if(this.salary > o.salary){
            return 1;
        }else if(this.salary < o.salary){
            return -1;
        }else{
            if(this.id > o.id){
                return 1;
            }else if(this.id < o.id){
                return -1;
            }else {
                return 0;
            }
        }
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}
